package com.liner.work;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Homework01 {
    static Scanner scanner = new Scanner(System.in);
    static Random random = new Random();

    public static void main(String[] args) {

        //1.完成CommandBySwitch命令解析程序———————————练习switch
        boolean flag = true;
        System.out.println("欢迎使用XX银行管理系统");
        System.out.println("功能介绍:1.存款业务 2.取款业务 3.查询余额业务 4.退卡");
        commandBySwitch(flag);

        //完成Guessing猜数字———————————————————练习while自造死循环

        System.out.println("系統随机生成两位数中...");
        guessingNumber();

        //输出1到100的累加和———————————————————练习for循环
        int sum = 0;
        for (int i = 1; i <= 100; i++) {
            sum += i;
        }
        System.out.println("sum: " + sum);

        //完成Addition随机加法运算符———————————————练习for循环
        int score = 0;
        System.out.println("两位数加法:");
        addition(score);


        //完成MuiltTable九九乘法表————————————————练习嵌套for循环
        muiltTable();

        //完成MaxOfArray求数组元素最大值并放在最后一位—————练习数组、循环
        System.out.print("请输入数组的长度:");
        int len = scanner.nextInt();
        maxOfArray(len);

        //完成生成N位验证码

        System.out.print("请输入想要生成的验证码位数:");
        len = scanner.nextInt();
        System.out.println(verCode(len));


        // 完成找到2到100之内的所有素数
        prime();

        System.out.println();
        //完成飞机票打折—————————————————————练习分支和方法
        System.out.print("请输入飞机票单价:");
        int price = scanner.nextInt();
        System.out.print("请输入当前月份:");
        int month = scanner.nextInt();
        System.out.print("请输入预购舱位（1.头等舱2.商务舱3.经济舱）:");
        int type = scanner.nextInt();

        double calFinalPrice = calFinalPrice(price, month, type);
        if (calFinalPrice != -1) {
            System.out.println("最终花费:" + calFinalPrice);
        }


        //完成评委打分:去掉最高分和最低分之后的平均分——————练习数组、循环和方法
        System.out.print("请输入本场有几位评委:");
        int count = scanner.nextInt();
        double[] scores = inputData(count);
        double avg = getAvg(scores);
        System.out.println("去掉最高分和最低分之后的平均分:" + avg);


    }

    /**
     * 获得平均分
     *
     * @param scores
     * @return
     */
    private static double getAvg(double[] scores) {

        return getSum(scores) / (scores.length - 2);
    }

    /**
     * 获得总分
     *
     * @param scores
     * @return
     */
    private static double getSum(double[] scores) {
        double sum = 0;
        Arrays.sort(scores);
        for (int i = 1; i < scores.length - 1; i++) {
            sum += scores[i];
        }
        return sum;
    }

    /**
     * 输入评委打分
     *
     * @param count
     * @return
     */
    private static double[] inputData(int count) {
        Scanner scanner = new Scanner(System.in);
        double[] scores = new double[count];
        for (int i = 0; i < count; i++) {
            System.out.print("第" + (i + 1) + "位评委的打分为:");
            scores[i] = scanner.nextDouble();
            if (scores[i] < 0 || scores[i] > 100) {
                System.out.println("请输入0~100区间的分数");
                break;
            }
        }
        return scores;
    }

    /**
     * 飞机票打折
     *
     * @param price
     * @param month
     * @param type
     */
    private static double calFinalPrice(int price, int month, int type) {
        double finalPrice;
        if (price < 0) {
            System.out.println("请输入正确价格");
            return -1;
        }
        if (month < 1 || month > 12) {
            System.out.println("请输入正确月份");
            return -1;
        }
        if (type < 1 || type > 3) {
            System.out.println("请输入正确舱位");
            return -1;
        }

        if (month >= 5 && month <= 10) {
            //旺季
            switch (type) {
                case 1:
                    finalPrice = price * 0.9;
                    break;
                case 2:
                    finalPrice = price * 0.85;
                    break;
                default:
                    finalPrice = price * 0.8;
            }

        } else {
            //淡季
            switch (type) {
                case 1:
                    finalPrice = price * 0.7;
                    break;
                case 2:
                    finalPrice = price * 0.65;
                    break;
                default:
                    finalPrice = price * 0.6;
            }
        }
        return finalPrice;

    }


    /**
     * 完成找到2到100之内的所有素数
     */
    private static void prime() {
        boolean flag;
        for (int i = 2; i <= 100; i++) {
            flag = true;
            for (int j = 2; j <= i / 2; j++) {
                if (i % j == 0) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                System.out.print(i + "\t");
            }

        }
    }


    /**
     * 完成生成N位验证码
     *
     * @param len
     * @return
     */
    private static String verCode(int len) {
        String code = "abcdefghijklmnopqrstuvwxyzABCDEFIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < len; i++) {
            int index = (int) (Math.random() * code.length());
            sb.append(code.charAt(index));
        }
        return sb.toString();
    }


    /**
     * 完成MaxOfArray求数组元素最大值并放在最后一位
     *
     * @param len
     */
    private static void maxOfArray(int len) {
        int[] arr = new int[len];

        for (int i = 0; i < arr.length; i++) {
            System.out.print("请输入第" + (i + 1) + "个元素的值:");
            arr[i] = scanner.nextInt();
        }
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (max < arr[i]) {
                max = arr[i];
            }
        }
        arr = Arrays.copyOf(arr, arr.length + 1);
        arr[arr.length - 1] = max;
        System.out.println(Arrays.toString(arr));
    }

    /**
     * 完成MuiltTable九九乘法表
     */
    private static void muiltTable() {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i + " * " + j + " = " + i * j + "\t");
            }
            System.out.println();
        }
    }

    /**
     * 完成Addition随机加法运算符
     *
     * @param score
     */
    private static void addition(int score) {
        for (int i = 1; i <= 10; i++) {
            int num1 = random.nextInt(100);
            int num2 = random.nextInt(100);
            System.out.println("第" + i + "题:" + num1 + " + " + num2 + "= ");
            System.out.print("你的答案是:");
            int result = scanner.nextInt();
            if (num1 + num2 == result) {
                System.out.println("恭喜你答对了");
                score += 10;
            } else {
                System.out.println("很遗憾答错了");
            }
        }
        System.out.println("本次得分：" + score);
    }

    /**
     * Guessing猜数字
     */
    private static void guessingNumber() {
        int rand = random.nextInt(100);
        while (true) {
            System.out.print("请输入你所猜的数字(0~99):");
            if (!scanner.hasNextInt()) {
                System.out.println("请输入数字....");
                break;
            }
            int number = scanner.nextInt();
            if (number == rand) {
                System.out.println("恭喜你猜对了");
                break;
            } else if (number < rand) {
                System.out.println("很遗憾猜小了");
            } else {
                System.out.println("很遗憾猜大了");
            }
        }
    }

    /**
     * 命令解析程序
     *
     * @param flag
     */
    private static void commandBySwitch(boolean flag) {
        while (flag) {
            System.out.print("请输入要操作的编号:");
            if (!scanner.hasNextInt()) {
                System.out.println("请输入数字....");
                break;
            }
            int command = scanner.nextInt();

            switch (command) {
                case 1:
                    System.out.println("存款业务....");
                    break;
                case 2:
                    System.out.println("取款业务....");
                    break;
                case 3:
                    System.out.println("查询余额业务...");
                    break;
                case 4:
                    System.out.println("退卡");
                    flag = false;
                    break;
                default:
                    System.out.println("输入错误....");
                    break;

            }
        }
    }

}
